package operate;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import operate.vo.Attr;

import util.Log;
import db.JdbcOperate;
import db.TableConstants;


public class AttrOper extends JdbcOperate{
	/**@Desc 根据表单id,查询出所有属性
	 * @param cid
	 * @return
	 */
	public static List<Attr> findAttrsById(String cid) {
		return findAttrsById(cid,null);
	}
	public static List<Attr> findAttrsById(String cid,String type) {
		List<Attr> formAttrs = new ArrayList<Attr>();
		StringBuffer sb=new StringBuffer();
		sb.append("select * from "+TableConstants.FORM_ATTR);
		sb.append("where isdelete='0' and cid="+cid);
		if("update".equals(type)){
			sb.append(" and update_view='1'");
		}
		if("list".equals(type)){
			sb.append(" and list_view='1'");
		}
		try {
			List<Map> list = JdbcOperate.query("select * from "
					+ TableConstants.FORM_ATTR + " where isdelete='0' and cid="
					+ cid);
			for (Map map : list) {
				Attr formAttr = new Attr();
				formAttr.setAname(map.get("ANAME").toString());
				formAttr.setDefault_value(map.get("DEFAULT_VALUE").toString());
				formAttr.setId(map.get("ID").toString());
				formAttr.setUpdate_view(map.get("UPDATE_VIEW").toString());
				formAttr.setLegth(map.get("LEGTH").toString());
				formAttr.setList_view(map.get("LIST_VIEW").toString());
				formAttr.setReg(map.get("REG").toString());
				formAttr.setReg_name(map.get("REG_NAME").toString());
				formAttr.setType(map.get("TYPE").toString());
				formAttr.setValue_list(map.get("VALUE_LIST").toString());
				formAttr.setTip(map.get("TIP").toString());
				formAttrs.add(formAttr);
			}
		} catch (SQLException e) {
			Log.write(3, AttrOper.class, "查询所有表单出现错误", e);
		}
		return formAttrs;
	}
	
	/**@Desc添加表单属性
	 * @param request
	 */
	public static void addAttr(ServletRequest request) {
		String aname = request.getParameter("aname");
		String type = request.getParameter("type");
		String valueList = request.getParameter("valueList");
		String defaultValue = request.getParameter("defaultValue");
		String listView = request.getParameter("listView");
		String updateView = request.getParameter("updateView");
		String tip = request.getParameter("tip");
		String regName = request.getParameter("regName");
		String reg = request.getParameter("reg");
		String cid = request.getParameter("cid");
		StringBuffer sb = new StringBuffer();
		try {
		// 插入表单表
			String id= JdbcOperate.getSeq(TableConstants.FORM_ATTR_SEQ);
		sb.append("insert into " + TableConstants.FORM_ATTR + "");
		sb.append("(aname,type,value_list,default_value,list_view,update_view,tip,reg_name,reg,cid,isdelete,id)values");
		sb.append("('" + aname + "','" + type + "','" + valueList + "','"
				+ defaultValue + "','" + listView + "','" + updateView + "','"
				+ tip + "','" + regName + "','" + reg + "'," + cid
				+ ",'0',"+id+");");
		// 添加表字段名称
		sb.append("alter table t"+cid+" add f"+id+" varchar(200)");
		
			JdbcOperate.batchInsertOrUpdate(sb.toString());
		} catch (SQLException e) {
			Log.warn(AttrOper.class, "添加表单出现错误", e);
		}
	}
	/**@Desc 根据表单属性id，返回一条数据
	 * @param id
	 * @return
	 */
	public static Map findAttrById(String id){
		StringBuffer sb=new StringBuffer();
		sb.append(" select * from ");
		sb.append(TableConstants.FORM_ATTR);
		sb.append(" where id=");
		sb.append(id);
		try {
			return JdbcOperate.query(sb.toString()).get(0);
		} catch (SQLException e) {
			Log.warn(AttrOper.class, "查询某属性一条记录出现异常", e);
		}
		return null;
	}
	
}
